package com.hantanger.weblog.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hantanger.weblog.model.Setting;
import com.hantanger.weblog.model.User;
import com.hantanger.weblog.service.PicasaService;
import com.hantanger.weblog.util.ClassFactory;
import com.hantanger.weblog.util.HtmlUtil;
import com.hantanger.weblog.util.UserHelper;

/**
 * 设置
 * @author lys
 *
 */
public class SettingController extends BaseController {
	
	private PicasaService getPicasaService(){
		return ClassFactory.getInstance(PicasaService.class);
	}

	@Override
	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("isSetting", true);
		if(!UserHelper.isAdmin()){
			request.setAttribute("errorMsg", "无此操作权限！");
			return "error";
		}
		Setting setting = getSettingDao().getDefaultSetting();
		request.setAttribute("setting", setting);
		return "setting";
	}
	
	@Override
	public String doPost(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("isSetting", true);
		if(!UserHelper.isAdmin()){
			request.setAttribute("errorMsg", "无此操作权限！");
			return "error";
		}
		String type = request.getParameter("type");
		if("google".equalsIgnoreCase(type)){
			processGoogle(request, response);
		}
		if("bloginfo".equalsIgnoreCase(type)){
			processBlogInfo(request, response);
		}
		if("addUser".equalsIgnoreCase(type)){
			processAddUser(request, response);
		}
		return null;
	}
	
	private void processGoogle(HttpServletRequest request, HttpServletResponse response){
		String uname = request.getParameter("uname");
		String passwd = request.getParameter("passwd");
		Setting defaultSetting = getSettingDao().getDefaultSetting();
		defaultSetting.setGoogleEmail(uname);
		defaultSetting.setGooglePassport(passwd);
		getSettingDao().update(defaultSetting);
		try{
			getPicasaService().init();
			renderJson(true, response, JSON);
		}catch(Exception e){
			e.printStackTrace();
			renderJson(false, response, JSON);
		}
	}
	
	private void processBlogInfo(HttpServletRequest request, HttpServletResponse response){
		String spacename = request.getParameter("spacename");
		String description = request.getParameter("description");
		String spaceUrl = request.getParameter("spaceUrl");
		Setting defaultSetting = getSettingDao().getDefaultSetting();
		defaultSetting.setSpaceName(spacename);
		defaultSetting.setDescription(description);
		defaultSetting.setSpaceUrl(spaceUrl);
		getSettingDao().update(defaultSetting);
		renderJson(true, response, JSON);
	}
	
	private void processAddUser(HttpServletRequest request, HttpServletResponse response){
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String nickname = HtmlUtil.getString(request, "nickname", "");
		boolean isadmin = HtmlUtil.getBool(request, "isadmin", false);
		int privilege = User.PRIVILEGE_AUTHOR;
		if(isadmin){
			privilege = User.PRIVILEGE_ADMIN;
		}
		User currentUser = getUserDao().getUserByEmail(email);
		if(currentUser != null){
			renderJson(false, response, JSON);
			return;
		}
		User user = new User();
		user.setEmail(email);
		user.setPassword(password);
		user.setPrivilege(privilege);
		user.setNickname(nickname);
		getUserDao().insert(user);
		renderJson(true, response, JSON);
		return;
	}
}
